package com.pearadmin.modules.sys.controller.manage;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.PoleInfo;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.service.IPoleInfoService;
import com.pearadmin.modules.sys.service.ISlotInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 极号信息Controller
 *
 * @author Mr.Ling
 * @date 2021-11-27
 */
@RestController
@Api(tags = {"极号信息"})
@RequestMapping(ControllerConstant.API_MANAGE_PREFIX + "poleInfo")
public class PoleInfoController extends BaseController {
    private String prefix = "manage/poleInfo" ;

    @Autowired
    private IPoleInfoService poleInfoService;

    @Autowired
    private ISlotInfoService slotInfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/manage/poleInfo/main','manage:poleInfo:main')")
    public ModelAndView main(Model model) {
        model.addAttribute("slots", slotInfoService.selectSlotInfoList(null));
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询极号信息列表
     */
    @ResponseBody
    @ApiOperation("列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/manage/poleInfo/data','manage:poleInfo:data')")
    public ResultTable list(@ModelAttribute PoleInfo poleInfo, PageDomain pageDomain) {
        PageInfo<PoleInfo> pageInfo = poleInfoService.selectPoleInfoPage(poleInfo, pageDomain);
        //todo 改成前端渲染
        //for (PoleInfo info : pageInfo.getList()) {
        //    SlotInfo slotInfo = slotInfoService.selectSlotInfoById(info.getSlotId());
        //    info.setSlotId(slotInfo.getSlotNo());
        //}
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 通过槽号获取极号信息
     * @param slotId
     * @return
     */
    @ResponseBody
    @GetMapping("/dataBySlotId")
    public Result dataBySlotId(String slotId) {
        PoleInfo poleInfo = new PoleInfo();
        poleInfo.setSlotId(slotId);
        return Result.success(poleInfoService.selectPoleInfoList(poleInfo));
    }

    /**
     * 新增极号信息
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/manage/poleInfo/add','manage:poleInfo:add')")
    public ModelAndView add(Model model) {
        model.addAttribute("slots", slotInfoService.selectSlotInfoList(null));
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存极号信息
     */
      @ResponseBody
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/manage/poleInfo/add','manage:poleInfo:add')")
    public Result save(@RequestBody PoleInfo poleInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        poleInfo.setCreateTime(LocalDateTime.now());
        poleInfo.setCreateBy(sysUser.getUserId());
        poleInfo.setCreateName(sysUser.getRealName());
        poleInfo.setId(SequenceUtil.makeStringId());
        return decide(poleInfoService.insertPoleInfo(poleInfo));
    }

    /**
     * 修改极号信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/manage/poleInfo/edit','manage:poleInfo:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        PoleInfo poleInfo = poleInfoService.selectPoleInfoById(id);
        poleInfo.setUpdateTime(LocalDateTime.now());
        mmap.put("poleInfo", poleInfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存极号信息
     */
    @ResponseBody
    @ApiOperation("更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/manage/poleInfo/edit','manage:poleInfo:edit')")
    public Result update(@RequestBody PoleInfo poleInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        poleInfo.setUpdateTime(LocalDateTime.now());
        poleInfo.setUpdateBy(sysUser.getUserId());
        poleInfo.setUpdateName(sysUser.getUsername());
        return decide(poleInfoService.updatePoleInfo(poleInfo));
    }

    /**
     * 删除极号信息
     */
    @ResponseBody
    @ApiOperation("批量刪除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/manage/poleInfo/remove','manage:poleInfo:remove')")
    public Result batchRemove(String ids) {
        return decide(poleInfoService.deletePoleInfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id 刪除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/manage/poleInfo/remove','manage:poleInfo:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(poleInfoService.deletePoleInfoById(id));
    }
}
